#![allow(non_snake_case)]

use prost::{bytes::Buf, DecodeError, Message};

pub trait MessageExt: Send + Sync {
    fn decode_length_delimited(buf: impl Buf) -> Result<Self, DecodeError>
    where
        Self: Default;
}

// impl<T1, T2> MessageExt for (T1, T2)
// where
//     T1: Message + Default + Send + Sync,
//     T2: Message + Default + Send + Sync,
// {
//     fn decode_length_delimited(mut buf: impl Buf) -> Result<Self, DecodeError> {
//         let t1 = T1::decode_length_delimited(&mut buf)?;

//         let t2 = T2::decode_length_delimited(&mut buf)?;

//         Ok((t1, t2))
//     }
// }



// 使用宏为不同大小的元组实现MessageExt
macro_rules! impl_message_ext_for_tuple {
    ($($T:ident),*) => {
        impl<$($T),*> MessageExt for ($($T),*)
        where
            $($T: Message + Default + Send + Sync,)*
        {
            fn decode_length_delimited(mut buf: impl Buf) -> Result<Self, DecodeError> {
                $(
                    let $T = $T::decode_length_delimited(&mut buf)?;
                )*
                Ok(($($T),*))
            }
        }
    };
}

// 为2到17元组生成实现
impl_message_ext_for_tuple!(T1, T2);
impl_message_ext_for_tuple!(T1, T2, T3);
impl_message_ext_for_tuple!(T1, T2, T3, T4);
impl_message_ext_for_tuple!(T1, T2, T3, T4, T5);
impl_message_ext_for_tuple!(T1, T2, T3, T4, T5, T6);
impl_message_ext_for_tuple!(T1, T2, T3, T4, T5, T6, T7);
impl_message_ext_for_tuple!(T1, T2, T3, T4, T5, T6, T7, T8);
impl_message_ext_for_tuple!(T1, T2, T3, T4, T5, T6, T7, T8, T9);
impl_message_ext_for_tuple!(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10);
impl_message_ext_for_tuple!(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11);
impl_message_ext_for_tuple!(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12);
impl_message_ext_for_tuple!(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13);
impl_message_ext_for_tuple!(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14);
impl_message_ext_for_tuple!(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15);
impl_message_ext_for_tuple!(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16);
impl_message_ext_for_tuple!(T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17);